package com.kasax.skin2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kasax.skin2.R;

public class CircleView extends View {
    private final AttributeSet attrs;
    private int corcleColorResId;
    private Paint mTextPaint;
    private int redius;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.attrs = attrs;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        corcleColorResId = typedArray.getResourceId(R.styleable.CircleView_corcelColor, 0);
        typedArray.recycle();

        mTextPaint = new Paint();
        //设置颜色
        mTextPaint.setColor(getResources().getColor(corcleColorResId));
        //开启抗锯齿，平滑文字和圆弧的边缘
        mTextPaint.setAntiAlias(true);
        //      //设置文本位于相对于原点的中间
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        //获取宽度一半
        int w = getWidth() / 2;
        //获取高度一半
        int h = getHeight() / 2;
        //设置半径为宽或者高的最小值
        redius = Math.min(w, h);
        //利用canvas画一个圆
        canvas.drawCircle(w, h, redius, mTextPaint);
    }

    public void setCorcleColor(@ColorInt int color) {
        mTextPaint.setColor(color);
        invalidate();
    }
}
